package com.ikonke.iot.alarm.service;

import java.io.IOException;
import java.util.Date;

import com.ikonke.iot.alarm.email.MailDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.ikonke.iot.alarm.es.EsRepository;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: liull
 * @Description: 任务调度服务
 * @Date: Created in 16:40 2020/6/5
 * @Modified By:
 */
@Slf4j
@Service
public class AlarmTask {

    @Autowired
    private EsRepository esRepository;

    @Autowired
    private MailService mailService;

    @Scheduled(cron = "0 1 0 * * ?")
    @Async("eventLoopGroup")
    public void alarm() {
        log.info("开始检测昨天新出现的异常日志");
        esRepository.getErrorMsgAndSend("1", "1", "2");
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    @Async("eventLoopGroup")
    public void init() throws IOException {
        log.info("开始每五分钟时的统计任务");
        esRepository.doInit();
    }

    @Scheduled(cron = "0 10 0 * * ?")
    @Async("eventLoopGroup")
    public void alarmYesterday() {
        log.info("开始统计昨天的的异常日志");
        esRepository.statErrorMsg(DateUtil.offsetDay(new Date(), -1).toJdkDate(), "1", "2");
    }

    @Scheduled(cron = "0 5 0 * * ?")
    @Async("eventLoopGroup")
    public void alarmSuddenlyIncreaseLogs() {
        log.info("开始统计暴增的日志的信息");
        esRepository.statSuddenlyIncreaseLogInfo();
    }

    // @Scheduled(cron = "0 0/2 * * * ?")
    // @Async("eventLoopGroup")
    // public void testSendEmail() {
    // log.info("test send email");
    // MailDO mailDO = new MailDO();
    // mailDO.setContent("测试一下linux发送邮件");
    // mailDO.setEmail("1442993929@qq.com");
    // mailDO.setTitle("test");
    // mailService.sendTextMail(mailDO);
    // }

    @Scheduled(cron = "0 1/10 * * * ?")
    @Async("eventLoopGroup")
    /**
     * coonectException立即告警
     */
    public void testLsc() {
        esRepository.alarmLsc();
    }

    @Scheduled(cron = "0 0/2 * * * ?")
    @Async("eventLoopGroup")
    public void recordPushMsg() throws IOException {
        log.info("开始记录主机推送消息");
        esRepository.recordPushMsg();
    }

}
